package com.liujiaqi.lease.web.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liujiaqi.lease.common.exception.LeaseException;
import com.liujiaqi.lease.common.result.ResultCodeEnum;
import com.liujiaqi.lease.model.entity.*;
import com.liujiaqi.lease.model.enums.ItemType;
import com.liujiaqi.lease.web.admin.mapper.*;
import com.liujiaqi.lease.web.admin.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liujiaqi.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.liujiaqi.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.liujiaqi.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.liujiaqi.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.liujiaqi.lease.web.admin.vo.fee.FeeValueVo;
import com.liujiaqi.lease.web.admin.vo.graph.GraphVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
  implements ApartmentInfoService {

  @Autowired
  private ApartmentInfoMapper apartmentInfoMapper;

  @Autowired
  private GraphInfoMapper graphInfoMapper;

  @Autowired
  private FacilityInfoMapper facilityInfoMapper;

  @Autowired
  private FeeValueMapper feeValueMapper;

  @Autowired
  private LabelInfoMapper labelInfoMapper;

  @Autowired
  private RoomInfoMapper roomInfoMapper;

  @Autowired
  private GraphInfoService graphInfoService;

  @Autowired
  private ApartmentFacilityService apartmentFacilityService;

  @Autowired
  private ApartmentLabelService apartmentLabelService;

  @Autowired
  private ApartmentFeeValueService apartmentFeeValueService;

  @Override
  public void saveOrUpdateApartment(ApartmentSubmitVo apartmentSubmitVo) {

    boolean isUpdate = apartmentSubmitVo.getId() != null;

    super.saveOrUpdate(apartmentSubmitVo);

    if (isUpdate) {
      // 1.删除图片列表
      LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
      graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
      graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId, apartmentSubmitVo.getId());
      graphInfoService.remove(graphInfoLambdaQueryWrapper);

      // 2.删除配套列表
      LambdaQueryWrapper<ApartmentFacility> apartmentFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
      apartmentFacilityLambdaQueryWrapper.eq(ApartmentFacility::getApartmentId, apartmentSubmitVo.getId());
      apartmentFacilityService.remove(apartmentFacilityLambdaQueryWrapper);

      // 3.删除标签列表
      LambdaQueryWrapper<ApartmentLabel> apartmentLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
      apartmentLabelLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId, apartmentSubmitVo.getId());
      apartmentLabelService.remove(apartmentLabelLambdaQueryWrapper);

      // 4.删除杂费列表
      LambdaQueryWrapper<ApartmentFeeValue> apartmentFeeValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
      apartmentFeeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getApartmentId, apartmentSubmitVo.getId());
      apartmentFeeValueService.remove(apartmentFeeValueLambdaQueryWrapper);
    }

    // 1.插入图片列表
    List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
    if (!CollectionUtils.isEmpty(graphVoList)) {
      ArrayList<GraphInfo> graphInfoList = new ArrayList<>();
      for (GraphVo graphVo : graphVoList) {
        GraphInfo graphInfo = new GraphInfo();
        graphInfo.setItemType(ItemType.APARTMENT);
        graphInfo.setItemId(apartmentSubmitVo.getId());
        graphInfo.setName(graphVo.getName());
        graphInfo.setUrl(graphVo.getUrl());
        graphInfoList.add(graphInfo);
      }
      graphInfoService.saveBatch(graphInfoList);
    }

    // 2.插入配套列表
    List<Long> facilityInfoIdList = apartmentSubmitVo.getFacilityInfoIds();
    if (!CollectionUtils.isEmpty(facilityInfoIdList)) {
      ArrayList<ApartmentFacility> facilityList = new ArrayList<>();
      for (Long facilityInfoId : facilityInfoIdList) {
        ApartmentFacility apartmentFacility = new ApartmentFacility();
        apartmentFacility.setApartmentId(apartmentSubmitVo.getId());
        apartmentFacility.setFacilityId(facilityInfoId);
        facilityList.add(apartmentFacility);
      }
      apartmentFacilityService.saveBatch(facilityList);
    }

    // 3.插入标签列表
    List<Long> labelIdList = apartmentSubmitVo.getLabelIds();
    if (!CollectionUtils.isEmpty(labelIdList)) {
      ArrayList<ApartmentLabel> apartmentLabelList = new ArrayList<>();
      for (Long labelId : labelIdList) {
        ApartmentLabel apartmentLabel = new ApartmentLabel();
        apartmentLabel.setApartmentId(apartmentSubmitVo.getId());
        apartmentLabel.setLabelId(labelId);
        apartmentLabelList.add(apartmentLabel);
      }
      apartmentLabelService.saveBatch(apartmentLabelList);
    }

    // 4.插入杂费列表
    List<Long> feeValueIdList = apartmentSubmitVo.getFeeValueIds();
    if (!CollectionUtils.isEmpty(feeValueIdList)) {
      ArrayList<ApartmentFeeValue> apartmentFeeValueList = new ArrayList<>();
      for (Long feeValueId : feeValueIdList) {
        ApartmentFeeValue apartmentFeeValue = new ApartmentFeeValue();
        apartmentFeeValue.setApartmentId(apartmentSubmitVo.getId());
        apartmentFeeValue.setFeeValueId(feeValueId);
        apartmentFeeValueList.add(apartmentFeeValue);
      }
      apartmentFeeValueService.saveBatch(apartmentFeeValueList);
    }
  }

  @Override
  public IPage<ApartmentItemVo> pageItem(Page<ApartmentItemVo> page, ApartmentQueryVo queryVo) {
    return apartmentInfoMapper.pageItem(page, queryVo);
  }

  @Override
  public ApartmentDetailVo getDetailById(Long id) {
    // 1.查询公寓信息
    ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(id);

    // 2.查询图片列表
    List<GraphVo> graphVoList = graphInfoMapper.selectListByItemTypeAndId(ItemType.APARTMENT, id);

    // 3.查询标签列表
    List<LabelInfo> labelInfoList = labelInfoMapper.selectListByApartmentId(id);

    // 4.查询配套列表
    List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectListByApartmentId(id);

    // 5.查询杂费列表
    List<FeeValueVo> feeValueVoList = feeValueMapper.selectListByApartmentId(id);

    // 6.封装结果
    ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
    BeanUtils.copyProperties(apartmentInfo, apartmentDetailVo);
    apartmentDetailVo.setGraphVoList(graphVoList);
    apartmentDetailVo.setLabelInfoList(labelInfoList);
    apartmentDetailVo.setFacilityInfoList(facilityInfoList);
    apartmentDetailVo.setFeeValueVoList(feeValueVoList);

    return apartmentDetailVo;
  }

  @Override
  public void removeApartmentById(Long id) {

    LambdaQueryWrapper<RoomInfo> roomInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
    roomInfoLambdaQueryWrapper.eq(RoomInfo::getApartmentId, id);
    Long count = roomInfoMapper.selectCount(roomInfoLambdaQueryWrapper);

    if (count > 0) {
      // 终止删除，并且响应提示信息
      throw new LeaseException(ResultCodeEnum.ADMIN_APARTMENT_DELETE_ERROR);
    }

    super.removeById(id);

    // 1.删除图片列表
    LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
    graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
    graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId, id);
    graphInfoService.remove(graphInfoLambdaQueryWrapper);

    // 2.删除配套列表
    LambdaQueryWrapper<ApartmentFacility> apartmentFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
    apartmentFacilityLambdaQueryWrapper.eq(ApartmentFacility::getApartmentId, id);
    apartmentFacilityService.remove(apartmentFacilityLambdaQueryWrapper);

    // 3.删除标签列表
    LambdaQueryWrapper<ApartmentLabel> apartmentLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
    apartmentLabelLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId, id);
    apartmentLabelService.remove(apartmentLabelLambdaQueryWrapper);

    // 4.删除杂费列表
    LambdaQueryWrapper<ApartmentFeeValue> apartmentFeeValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
    apartmentFeeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getApartmentId, id);
    apartmentFeeValueService.remove(apartmentFeeValueLambdaQueryWrapper);
  }
}




